package cn.edu.swu.datanalysis.dao;

import java.util.ArrayList;
import java.util.List;

import org.bson.Document;

import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;

import cn.edu.swu.datanalysis.entity.Record;

public class FetchRecords {
	MongoClient client  = null;

	public List<Record> getRecordsByPage(int pageNumber,int pageSize)
	{
		List<Record> records = new ArrayList<Record>();
		long start = System.nanoTime();

		client = new MongoClient("localhost", 27017);
		MongoDatabase db = client.getDatabase("mydb");
		MongoCollection<Document> cols = db.getCollection("mycol");
		FindIterable<Document> it = cols.find().skip(pageSize*(pageNumber-1)).limit(pageSize);
		MongoCursor<Document> cursor =  it.iterator();
		int count = 0;
		while(cursor.hasNext())
		{
			Document doc = cursor.next();
			Record record = new Record();
			record.setA1(doc.getDouble("a1"));
			record.setA2(doc.getDouble("a2"));
			record.setA3(doc.getDouble("a3"));
			record.setA4(doc.getDouble("a4"));
			record.setA5(doc.getDouble("a5"));
			record.setA6(doc.getDouble("a6"));
			record.setA7(doc.getDouble("a7"));
			record.setA8(doc.getDouble("a8"));
			record.setA9(doc.getDouble("a9"));
			record.setA10(doc.getDouble("a10"));
			record.setA11(doc.getDouble("a11"));
			record.setA12(doc.getDouble("a12"));
			record.setQuality(doc.getString("quality"));
			records.add(record);
			
			//System.out.println(cursor.next().toJson());
			count ++;
			
		}
		System.out.println("this turn find "+count+" records. ");
		long end = System.nanoTime();
		System.out.println("the run time is "+(end-start)/1000000+" ms.");
		return records;
	}
}
